﻿using StripeDemo.Models;
using StripeDemo.Utils;
using StripeNET;
using StripeNET.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace StripeDemo.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/
        public ActionResult Index()
        {
            // get all customer and put to view
            return View(Util.getAllCustomer());
        }

        //
        // GET: /Customer/CreateCustomer
        public ActionResult CreateCustomer()
        {
            return View();
        }
        // POST: /Customer/Create
        [HttpPost]
        public ActionResult Create()
        {
            if (HttpContext.Request.RequestType == "POST")
            {
                ViewBag.Submitted = true;
                // get information from form
                string fullname = Request.Form[Constant.FULLNAME].ToString();
                string billingaddress = Request.Form[Constant.BILLING_ADDRESS].ToString();
                string phonenumber = Request.Form[Constant.PHONE_NUMBER].ToString();
                string email = Request.Form[Constant.EMAIL].ToString();
                string extracustomerinfo = Request.Form[Constant.EXTRA_CUSTOMER_INFO].ToString();
                StripeCustomerCreateOptions newCustomer = new StripeCustomerCreateOptions();
                newCustomer.Email = email;
                //Put information not defined to metadata tag
                Dictionary<string, string> metadata = new Dictionary<string, string>();
                metadata.Add(Constant.EXTRA_CUSTOMER_INFO, extracustomerinfo);
                newCustomer.Metadata = metadata;
                StripeShipping shipping = new StripeShipping()
                {
                    SetAddress = billingaddress,
                    SetFullName = fullname,
                    SetPhone = phonenumber
                };
                newCustomer.Shipping = shipping;

                //POST object to Stripe and get object responsed from Stripe
                StripeCustomerService customerService = new StripeCustomerService();
                StripeCustomer stripeCustomer = customerService.Create(newCustomer, null);

                // Put information to view
                ViewBag.Id = stripeCustomer.CustomerId;
                ViewBag.FullName = fullname;
                ViewBag.BillingAddress = billingaddress;
                ViewBag.PhoneNumber = phonenumber;
                ViewBag.Email = email;
                ViewBag.ExtraInfo = extracustomerinfo;
            }
            return View();
        }

        // POST: /Customer/Edit
        [HttpPost]
        public ActionResult Save(string CustomerId)
        {
            // get information from form
            string fullname = Request.Form[Constant.FULLNAME].ToString();
            string billingaddress = Request.Form[Constant.BILLING_ADDRESS].ToString();
            string phonenumber = Request.Form[Constant.PHONE_NUMBER].ToString();
            string email = Request.Form[Constant.EMAIL].ToString();
            string extracustomerinfo = Request.Form[Constant.EXTRA_CUSTOMER_INFO].ToString();
            StripeCustomerUpdateOptions updateCustomer = new StripeCustomerUpdateOptions();
            updateCustomer.Email = email;
            //Put information not defined to metadata tag
            Dictionary<string, string> metadata = new Dictionary<string, string>();
            metadata.Add(Constant.EXTRA_CUSTOMER_INFO, extracustomerinfo);
            StripeShipping shipping = new StripeShipping()
            {
                SetAddress = billingaddress,
                SetFullName = fullname,
                SetPhone = phonenumber
            };
            updateCustomer.Shipping = shipping;
            updateCustomer.Metadata = metadata;
            // update customer and put to view
            StripeCustomerService stripeCustomerService = new StripeCustomerService();
            StripeCustomer customer = stripeCustomerService.Update(CustomerId, updateCustomer);
            if (customer == null)
            {
                return HttpNotFound();
            }
            ViewBag.HelpText = "Edit customer successfully!!!";
            return View("Edit", Util.convertToCustomer(customer));
        }

        public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            // get information of customer and put to view
            StripeCustomer stripeCustomer = Util.getUserInfoById(id);
            if (stripeCustomer == null)
            {
                return HttpNotFound();
            }
            ViewBag.HelpText = "Edit customer";
            return View(Util.convertToCustomer(stripeCustomer));
        }

        public ActionResult Delete(string id)
        {
            StripeCustomerService customerService = new StripeCustomerService();
            // delete customer, return to list customer page
            customerService.Delete(id);
            return RedirectToAction("Index" , "Customer");

        }
    }
}